package com.zhaosc.elasticsearch;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.Test;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-multi-get.html
 * 
 * @author zhaosc
 *
 */
public class MultiGetTest extends BaseTest {

	private static final Logger logger = Logger.getLogger(BaseTest.class);

	@Test
	public void testMultiGet() {

		MultiGetRequest request = new MultiGetRequest();
		request.add(new MultiGetRequest.Item(products, "1"));
		request.add(new MultiGetRequest.Item(products, "2"));
//		String[] includes = new String[] {"desc", "*r"};
//		String[] excludes = Strings.EMPTY_ARRAY;
//		FetchSourceContext fetchSourceContext =
//		        new FetchSourceContext(true, includes, excludes);
//		request.add(new MultiGetRequest.Item("index", "example_id")
//		    .fetchSourceContext(fetchSourceContext));
		try {
			MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
			MultiGetItemResponse[] items = response.getResponses();
			
			for (int i = 0; i < items.length; i++) {
				logger.info(items[i].getResponse());
			}
			
		} catch (IOException e) {
			logger.error("", e);
		}

	}
}
